﻿using System;
using System.Collections;

namespace Storm.TextEditor.Parser.Objects.Collections
{
	/// <summary>
	/// Represents a basic collection.
	/// </summary>
	public abstract class BaseCollection
		: CollectionBase, IList
	{
		#region Fields

		#region Events

		/// <summary>
		/// Occurs when an item is added to the BaseCollection.
		/// </summary>
		public event CollectionEventHandler ItemAdded = null;

		/// <summary>
		/// Occurs when an item is removed from the BaseCollection.
		/// </summary>
		public event CollectionEventHandler ItemRemoved = null;

		/// <summary>
		/// Occurs when the items of the BaseCollection are cleared.
		/// </summary>
		public event EventHandler ItemsCleared = null;

		#endregion

		#endregion

		#region Methods

		#region Protected

		/// <summary>
		/// Raises the ItemAdded event.
		/// </summary>
		/// <param name="index">Index value.</param>
		/// <param name="item">Item object.</param>
		protected virtual void OnItemAdded(int index, object item)
		{
			if (this.ItemAdded != null)
			{
				CollectionEventArgs e = new CollectionEventArgs(item, index);
				this.ItemAdded(this, e);
			}
		}

		/// <summary>
		/// Raises the ItemRemoved event.
		/// </summary>
		/// <param name="index">Index value.</param>
		/// <param name="item">Item object.</param>
		protected virtual void OnItemRemoved(int index, object item)
		{
			if (this.ItemRemoved != null)
			{
				CollectionEventArgs e = new CollectionEventArgs(item, index);
				this.ItemRemoved(this, e);
			}
		}

		/// <summary>
		/// Raises the ItemsCleared event.
		/// </summary>
		protected virtual void OnItemsCleared()
		{
			if (this.ItemsCleared != null)
				this.ItemsCleared(this, EventArgs.Empty);
		}

		protected override void OnClearComplete()
		{
			base.OnClearComplete();
			this.OnItemsCleared();
		}

		protected override void OnRemoveComplete(int index, object value)
		{
			base.OnRemoveComplete(index, value);
			this.OnItemRemoved(index, value);
		}

		protected override void OnInsertComplete(int index, object value)
		{
			base.OnInsertComplete(index, value);
			this.OnItemAdded(index, value);
		}

		#endregion

		#endregion

		/// <summary>
		/// Initializes a new instance of BaseCollection.
		/// </summary>
		public BaseCollection()
		{
		}
	}
}
